package com.imedway.ihe.dd.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.imedway.ihe.dd.dto.ProvidedRegisterDTO;
import com.imedway.ihe.dd.helper.Iti41ProvidedRegisterHelper;
import com.imedway.ihe.dd.properties.ItiAddressProperties;
import com.imedway.ihe.dd.result.ResponseResult;
import com.imedway.ihe.dd.service.Iti41ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.support.DefaultExchange;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
import org.openehealth.ipf.platform.camel.core.util.Exchanges;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.XdsRenderingUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/4
 */
@Slf4j
@RestController
@RequestMapping("/ihe/xds")
@RequiredArgsConstructor
@Api(tags = "XDS-Iti41文档提供与注册")
public class Iti41ClientController {

    private final CamelContext camelContext;
    private final ProducerTemplate producerTemplate;
    private final Iti41ClientService iti41ClientService;
    private final ItiAddressProperties itiAddressProperties;

    @ApiOperation(value = "ITI41-文档提供与注册")
    @ApiOperationSupport(order = 1)
    @PostMapping(value = "iti41", consumes = "application/json")
    public ResponseResult iti4142ProvidedAndRegister(@RequestBody ProvidedRegisterDTO providedRegisterDTO) throws Exception {
        this.iti41ClientService.CreateProvidedAndRegister(providedRegisterDTO);
        return ResponseResult.success();
    }

    @ApiOperation(value = "ITI41-文档提供与注册-提交单个文档")
    @ApiOperationSupport(order = 1)
    @PostMapping(value = "iti41-only-doc", consumes = "application/json")
    public Response iti4142ProvidedAndRegisterOnlyDocument(@RequestBody ProvidedRegisterDTO providedRegisterDTO) throws Exception {
        Exchange exchange = new DefaultExchange(camelContext);
        exchange.setPattern(ExchangePattern.InOut);

        // 构建数据
        ProvideAndRegisterDocumentSet provide = Iti41ProvidedRegisterHelper.createDocumentOnly(providedRegisterDTO);

        // 发送请求
        exchange.getIn().setBody(provide);

        log.debug("请求参数: \n" + XdsRenderingUtils.render(exchange));

        exchange = producerTemplate.send("xds-iti41://" + itiAddressProperties.getIti41Location(), exchange);

        Exception exception = Exchanges.extractException(exchange);
        if (exception != null) {
            log.error(ExceptionUtil.stacktraceToString(exception));
        }
        return exchange.getMessage().getMandatoryBody(Response.class);
    }

    @ApiOperation(value = "ITI41-文档提供与注册-提交多个文档")
    @ApiOperationSupport(order = 1)
    @PostMapping(value = "iti41-three-doc", consumes = "application/json")
    public ResponseResult iti4142ProvidedAndRegisterOnlyThreeDocument(@RequestBody ProvidedRegisterDTO providedRegisterDTO) throws Exception {
        this.iti41ClientService.CreateProvidedAndRegisterOnlyThreeDocument(providedRegisterDTO);
        return ResponseResult.success();
    }





}
